<?php
    session_start();
    require_once 'public/includes/functions.php';
    require_once 'models/container.php';
    
    class AddContainerController extends functions{
        var $container = null;
        var $valueEdit = null;
        
        function __construct(){
            if(empty($_SESSION['name'])){
                echo '<script language="javascript">
                        alert("Vui lòng đăng nhập để vào trang Admin");
                        document.location="index.php";
                    </script>';
            }
            $this->container = new Container_Model();            
        }
        
        public function addAction(){
            if(isset($_POST['addnew'])){
                if(is_uploaded_file($_FILES['fileanh']['tmp_name'])){
                    $filename=$_FILES['fileanh']['name'];
                    $dotpos=strpos($filename,".");
                    $duoi=substr($filename,$dotpos+1);
                    $newfile=rand(1,1000).".".$duoi;		
                    @copy($_FILES['fileanh']['tmp_name'],"upload/images/".$newfile);
                }
                $date = @date ("l, d F Y, h:i:s A", mktime());
                
                $ten=$_POST['txtten'];
                $gioithieu=$_POST['txtgioithieu'];
                $filenoidung=$_FILES['filenoidung']['name'];
                // move_uploaded_file($_FILES['filegioithieu']['tmp_name'],"file/".$filegioithieu);
                move_uploaded_file($_FILES['filenoidung']['tmp_name'],"upload/files/".$filenoidung);
                $diadiem=$_POST['txtdiadiem'];
                $sdt=$_POST['txtsdt'];
                $catid=$_POST['catid'];
                $nguon=$_POST['txtnguon'];
                $kinh=$_POST['txtkinhdo'];
                $vi=$_POST['txtvido'];                
                $this->container->add($ten,$gioithieu,$filenoidung,$diadiem,$sdt,$newfile,
                                        $nguon,$catid,$date,$vi,$kinh);                
                header('location: ./?rt=admin');
            }
                    
        }
        
        public function editAction(){
                $hinh = null;
                $id= $_POST['txtid'];
                if($_FILES['fileanh']['error'] <=0 ){                                    
                    $hinh=$_FILES['fileanh']['name'];                    		
                    move_uploaded_file($_FILES['fileanh']['tmp_name'],"upload/images/".$hinh);
                }
                $date = @date ("l, d F Y, h:i:s A", mktime());                
                $ten=$_POST['txtten'];
                $gioithieu=$_POST['txtgioithieu'];
                $filenoidung=$_FILES['filenoidung']['name'];                
                move_uploaded_file($_FILES['filenoidung']['tmp_name'],"upload/files/".$filenoidung);
                $diadiem=$_POST['txtdiadiem'];
                $sdt=$_POST['txtsdt'];
                $catid=$_POST['catid'];
                $nguon=$_POST['txtnguon'];
                $kinh=$_POST['txtkinhdo'];
                $vi=$_POST['txtvido'];   
                $this->container->edit($ten,$gioithieu,$filenoidung,$diadiem,$sdt,$hinh,$nguon,
                                        $catid, $date,$vi,$kinh,$id
                                        );
                header('location: ./?rt=admin');
        }
        
        public function getValueEdit(){
            $this->valueEdit = $this->container->getAll('container','id ='.$_GET['id']);
        }
    
    }
       
    $addContainer = new AddContainerController();
    //
    //load thong tin len de sua
    if(isset($_GET['action']) && $_GET['action']=='edit'){
        $addContainer->getValueEdit();
    }
    
    //
    //thuc hien sua hoac them
    if(isset($_POST['addnew'])){
        if($_POST['action']=='edit'){
            $addContainer->editAction();
        }else{
            $addContainer->addAction();    
        }
        
    }
    $addContainer->show('addContainer');
?>